package com.ruoyi.bigScreen;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.bigScreen.vo.EmphasisPeopleVO;
import com.ruoyi.common.core.domain.zzw.TEmphasisPeople;
import com.ruoyi.common.core.domain.zzw.TIndicatorsInfo;
import com.ruoyi.common.enums.*;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.CountVO;
import com.ruoyi.system.domain.vo.DisposeStatusVO;
import com.ruoyi.system.domain.vo.WarningLevelVO;
import com.ruoyi.system.service.ITEventManageService;
import com.ruoyi.system.service.ITIndicatorsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.ITEmphasisPeopleService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 重点人员Controller
 *
 * @author ruoyi
 * @date 2023-11-15
 */
@Api(tags = "重点人员管理")
@RestController
@RequestMapping("/people")
public class TEmphasisPeopleController extends BaseController {
    @Autowired
    private ITEmphasisPeopleService tEmphasisPeopleService;
    @Autowired
    private ITEventManageService itEventManageService;
    @Autowired
    private ITIndicatorsInfoService indicatorsInfoService;

    /**
     * 查询重点人员列表
     */
    @PreAuthorize("@ss.hasPermi('system:people:list')")
    @ApiOperation("查询重点人员列表")
    @GetMapping("/list")
    public TableDataInfo list(TEmphasisPeople tEmphasisPeople) {
        startPage();
        List<TEmphasisPeople> list = tEmphasisPeopleService.selectTEmphasisPeopleList(tEmphasisPeople);
        return getDataTable(list);
    }

    /**
     * 导出重点人员列表
     */
    @PreAuthorize("@ss.hasPermi('system:people:export')")
    @Log(title = "重点人员", businessType = BusinessType.EXPORT)
    @ApiOperation("导出重点人员列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, TEmphasisPeople tEmphasisPeople) {
        List<TEmphasisPeople> list = tEmphasisPeopleService.selectTEmphasisPeopleList(tEmphasisPeople);
        ExcelUtil<TEmphasisPeople> util = new ExcelUtil<TEmphasisPeople>(TEmphasisPeople.class);
        util.exportExcel(response, list, "重点人员数据");
    }

    /**
     * 获取重点人员详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:people:query')")
    @ApiOperation("获取重点人员详细信息")
    @GetMapping(value = "/{cPeopleId}")
    public AjaxResult getInfo(@PathVariable("cPeopleId") Long cPeopleId) {
        return success(tEmphasisPeopleService.selectTEmphasisPeopleByCPeopleId(cPeopleId));
    }

    /**
     * 新增重点人员
     */
    @PreAuthorize("@ss.hasPermi('system:people:add')")
    @Log(title = "重点人员", businessType = BusinessType.INSERT)
    @ApiOperation("新增重点人员")
    @PostMapping
    public AjaxResult add(@RequestBody TEmphasisPeople tEmphasisPeople) {
        return toAjax(tEmphasisPeopleService.insertTEmphasisPeople(tEmphasisPeople));
    }

    /**
     * 修改重点人员
     */
    @PreAuthorize("@ss.hasPermi('system:people:edit')")
    @Log(title = "重点人员", businessType = BusinessType.UPDATE)
    @ApiOperation("修改重点人员")
    @PutMapping
    public AjaxResult edit(@RequestBody TEmphasisPeople tEmphasisPeople) {
        return toAjax(tEmphasisPeopleService.updateTEmphasisPeople(tEmphasisPeople));
    }

    /**
     * 删除重点人员
     */
    @PreAuthorize("@ss.hasPermi('system:people:remove')")
    @ApiOperation("删除重点人员")
    @Log(title = "重点人员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cPeopleIds}")
    public AjaxResult remove(@PathVariable Long[] cPeopleIds) {
        return toAjax(tEmphasisPeopleService.deleteTEmphasisPeopleByCPeopleIds(cPeopleIds));
    }

    /**
     * 通过重点人员ID获取所有关联事件列表
     */
    @GetMapping("/manageByPeoId/{id}")
    @ApiOperation("通过重点人员ID获取所有关联事件列表")
    public AjaxResult manageByPeoId(@PathVariable Integer id) {
        return success(itEventManageService.manageByPeoId(id));
    }

    /**
     * 通过重点人员ID获取近一年事件数
     */
    @GetMapping("/manageByPeoIdRecently/{id}")
    @ApiOperation("通过重点人员ID获取近一年事件数")
    public AjaxResult manageByPeoIdRecently(@PathVariable Integer id) {
        return success(itEventManageService.manageByPeoIdRecently(id));
    }

    @GetMapping("/groupByHighTag")
    @ApiOperation("潜在关注重点人动态")
    public AjaxResult groupByHighTag() {
        List<CountVO> countList = tEmphasisPeopleService.groupByHighTag();
        return success(countList);
    }

    @GetMapping("/disposeCount")
    @ApiOperation("风险预警处置")
    public AjaxResult disposeCount() {
        DisposeStatusVO vo = tEmphasisPeopleService.disposeCount();
        return success(vo);
    }

    @GetMapping("/waringLevelCount")
    @ApiOperation("风险预警等级")
    public AjaxResult waringLevelCount() {
        WarningLevelVO vo = tEmphasisPeopleService.waringLevelCount();
        return success(vo);
    }

    @ApiOperation("重点人员列表")
    @GetMapping("/pageEmphasisPeople")
    public AjaxResult pageEmphasisPeople() {
        Page<TEmphasisPeople> page = tEmphasisPeopleService.pageEmphasisPeople();
        Page<EmphasisPeopleVO> voPage = new Page<>();
        BeanUtils.copyProperties(page, voPage);
        List<EmphasisPeopleVO> voList = new ArrayList<>();
        List<TEmphasisPeople> records = page.getRecords();
        for (TEmphasisPeople record : records) {
            EmphasisPeopleVO vo = new EmphasisPeopleVO();
            BeanUtils.copyProperties(record, vo);
            vo.setCPeopleTypeName(PeopleType.getNameByCode(record.getcPeopleType()));
            vo.setCWarningLevelName(WarningLevel.getNameByCode(record.getcWarningLevel()));
            vo.setCDisposeStatusName(DisposeStatus.getNameByCode(record.getcDisposeStatus()));
            voList.add(vo);
        }
        voPage.setRecords(voList);
        return AjaxResult.success(voPage);
    }

    @ApiOperation("风险趋势分析")
    @GetMapping("/getRiskTrends")
    public AjaxResult getRiskTrends() {
        TIndicatorsInfo indicator = indicatorsInfoService.getByIndicatorsId(37067L);
        return AjaxResult.success(indicator);
    }


    @ApiOperation("重点人员基本信息和标签")
    @GetMapping("/get")
    public AjaxResult get(Long cPeopleId) {
        TEmphasisPeople people = tEmphasisPeopleService.selectTEmphasisPeopleByCPeopleId(cPeopleId);
        EmphasisPeopleVO vo = new EmphasisPeopleVO();
        BeanUtils.copyProperties(people, vo);
        vo.setCPeopleTypeName(PeopleType.getNameByCode(vo.getcPeopleType()));
        vo.setCDisposeStatusName(DisposeStatus.getNameByCode(vo.getcDisposeStatus()));
        vo.setCWarningLevelName(WarningLevel.getNameByCode(vo.getcWarningLevel()));
        vo.setCWorkStatusName(WorkStatus.getNameByCode(vo.getcWorkStatus()));
        return success(vo);
    }

}
